######################################################
# REPLICATION FILE FOR
# "Sustaining Capitalism and Democracy: Lessons from 
#     Global Competition Policy"
# By Michael O Allen and Kenneth Scheve
#
# IN THIS FILE:
#   Event history analysis
######################################################

library(data.table)
library(survival)

setwd('/Users/moda/Desktop/ISR Replication/')

fullData <- fread('Data/event_history.csv')

# Cox Crisis --------------------------------------------------------------

evhist_bivar <- coxph(Surv(leg_gt0, leg_gt, competition_law_coded) ~
                        fin_crisis_rollmean_5 +
                        strata(leg_counter) +
                        frailty(code_cowc, distribution = 'gamma', method = 'em'),
                      ties = 'efron',
                      data = fullData)

evhist_cntrl0 <- coxph(Surv(leg_gt0, leg_gt, competition_law_coded) ~
                         fin_crisis_rollmean_5 +
                         dem_bin +
                         strata(leg_counter) +
                         frailty(code_cowc, distribution = 'gamma', method = 'em'),
                       ties = 'efron',
                       data = fullData)

evhist_cntrl1 <- coxph(Surv(leg_gt0, leg_gt, competition_law_coded) ~
                         fin_crisis_rollmean_5 +
                         dem_bin +
                         gdppc_ln +
                         pop_ln +
                         strata(leg_counter) +
                         frailty(code_cowc, distribution = 'gamma', method = 'em'),
                       ties = 'efron',
                       data = fullData)

evhist_cntrl2 <- coxph(Surv(leg_gt0, leg_gt, competition_law_coded) ~
                         fin_crisis_rollmean_5 +
                         dem_bin +
                         gdppc_ln +
                         pop_ln +
                         trade_open_ln +
                         strata(leg_counter) +
                         frailty(code_cowc, distribution = 'gamma', method = 'em'),
                       ties = 'efron',
                       data = fullData)

evhist_cntrl3 <- coxph(Surv(leg_gt0, leg_gt, competition_law_coded) ~
                         fin_crisis_rollmean_5 +
                         dem_bin +
                         gdppc_ln +
                         pop_ln +
                         trade_open_ln +
                         strata(leg_counter) +
                         top1Share_percentdiff +
                         frailty(code_cowc, distribution = 'gamma', method = 'em'),
                       ties = 'efron',
                       data = fullData)

texreg::texreg(list(evhist_bivar,
                    evhist_cntrl0,
                    evhist_cntrl1,
                    evhist_cntrl2,
                    evhist_cntrl3),
               stars = c(0.01, 0.05, 0.1),
               digits = 3,
               include.zph = FALSE,
               booktabs = TRUE)
